<!-- Horizontal Form -->
<div class="box box-info">
    <div class="box-header with-border">
        <h3 class="box-title">添加用户</h3>
        <h3 class="msg"></h3>
    </div>
    <!-- /.box-header -->
    <!-- form start -->
    <form class="form-horizontal">
        <div class="box-body">

            <div class="form-group">
                <label for="usernameId" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                    <input type="text" name="username" class="form-control" id="usernameId">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label">性别</label>
                <div class="col-sm-10">
                    <input type="radio" value="男" name="sex" title="男">男</input>&nbsp;
                    <input type="radio" value="女" name="sex" title="女">女</input>
                    <!--//取radio的值
                        $("input[name='radioName'][checked]").val();-->
                </div>
            </div>

            <div class="form-group">
                <label for="birthday" class="col-sm-2 control-label">生日</label>
                <div class="col-sm-10">
                    <input type="date" id="birthday">
                </div>
            </div>

            <div class="form-group">
                <label for="emailId" class="col-sm-2 control-label">邮箱</label>
                <div class="col-sm-10">
                    <input type="text" name="email" class="form-control" id="emailId">
                </div>
            </div>
            <div class="form-group">
                <label for="phoneId" class="col-sm-2 control-label">手机号</label>
                <div class="col-sm-10">
                    <input type="text" name="mobile" class="form-control" id="phoneId">
                </div>
            </div>

        </div>
        <!-- /.box-body -->
        <div class="box-footer">
            <button type="button" onclick="doCancel()" class="btn btn-default btn-cancel">取消</button>
            <button type="button" onclick="doUpdate()" class="btn btn-info pull-right btn-save">保存</button>
        </div>
        <!-- /.box-footer -->
    </form>
    <div class="layui-layer layui-layer-page layui-layer-molv layer-anim" id="treeLayer" type="page" times="2"
         showtime="0" contype="object"
         style="z-index:59891016; width: 300px; height: 450px; top: 100px; left: 500px; display:none">
        <span class="layui-layer-setwin"> <a
                class="layui-layer-ico layui-layer-close layui-layer-close1 btn-cancel"></a></span>
        <div class="layui-layer-btn layui-layer-btn-">
            <a class="layui-layer-btn0 btn-confirm">确定</a>
            <a class="layui-layer-btn1 btn-cancel">取消</a>
        </div>
    </div>
</div>
<script type="text/javascript" src="bower_components/ztree/jquery.ztree.all.min.js"></script>
<script type="text/javascript" src="bower_components/layer/layer.js"></script>
<script type="text/javascript">

    $(document).ready(function () {
        //页面加载完成以后加载角色信息并初始化页面
        getUserData();
    });

    // 取消按钮逻辑 -已完成
    function doCancel() {
        $("#mainContentId").load("user/info_list");
    }

    // 获取当前登录的用户对象- 已完成
    function getUserData() {
        let id = getCurrentUserData();
        let username, sex, birthday, email, mobile;
        $.ajax({
            url: `user/doShowUserInfo/${id}`,
            async: false,
            success(result) {
                username = result.username;
                sex = result.sex;
                birthday = result.birthday.substr(0, 10);
                email = result.email;
                mobile = result.mobile;
            }
        })
        $("#usernameId").val(username);
        setSex(sex);
        $("#birthday").val(birthday);
        $("#emailId").val(email);
        $("#phoneId").val(mobile);
        return userData;
    }

    // 根据查询到的性别决定选择哪个框
    function setSex(sex) {
        if (sex == "男") {
            $("input[name='sex'][value='男']").attr("checked", true);
        } else {
            $("input[name='sex'][value='女']").attr("checked", true);
        }
    }

    function doUpdate() {
        //1.获取表单数据
        let params = doGetEditFormData();
        let id = getCurrentUserData();
        //2.发起异步请求
        var updateUrl = `user/doUpdateUserInfo/${id}`;
        console.log(params);
        $.post(updateUrl, params, function (result) {
            if (result.state == 1) {
                alert(result.message);
                doCancel();
            } else {
                alert(result.message);
            }
        })
        doGetUserInfoObjects();
    }


    // 获取表单当前的数据
    function doGetEditFormData() {
        //获取用户输入的数据
        var params = {
            username: $("#usernameId").val(),
            sex: $('input:radio[name="sex"]:checked').val(),
            birthday: $("#birthday").val(),
            email: $("#emailId").val(),
            mobile: $("#phoneId").val(),
        }
        return params;
    }


</script>
    
    
    
    
    
    
    